/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vdg.lockapp;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

import com.vdg.data.SettingData;
import com.vdg.data.SettingData.LOCK_SETTING_TYPE;
import com.vdg.data.SettingData.LOCK_TYPE;
import com.vdg.lockapp.PassCodeActivity.PASS_CODE_ACTION_TYPE;
import com.vdg.lockapp.service.LockService;
import com.vdg.lockapp.service.LockService.LocalBinder;
import com.vdg.ui.view.TextViewPlus;

public class SettingFragment extends Fragment {
	public static final int REQ_CREATE_PATTERN = 1;
	private LockService mService;
	private TextViewPlus mTxvPattern;
	private TextViewPlus mTxvHidePhotos;
	private RelativeLayout mRlSetting;
	private TableLayout mTbSetting;
	private TableLayout mTbPassCode;
	private TableLayout mTbLockPatternSetting;
	private RadioButton mRbFakeForceClose;
	private RadioButton mRbTimeSetting;
	private RadioButton mRbPassCodeSetting;
	private RadioButton mRbHomeSetting;
	private RadioButton mRbExitSetting;
	private RadioButton mRbLockPatternSetting;
	private RelativeLayout mRlLockPatternSetting;
	private RelativeLayout mRlRequirePassword;
	private RelativeLayout mRlPassCodeSetting;
	private LinearLayout mLlHomeSetting;
	private LinearLayout mLlExitSetting;
	private CheckBox mChbRequirePassword;
	private SettingData mSettingData;
	private LinearLayout mLnRateForUS;
	private RelativeLayout mRlFakeForceClose;
	private TableRow mTrChangePassWord;
	View mContentView;
	private static final String TAG = "CopyOfFragmentStackSupport";
	private ServiceConnection mServiceConnection = new ServiceConnection() {
		@TargetApi(Build.VERSION_CODES.HONEYCOMB)
		// required for Sherlock's invalidateOptionsMenu */
		public void onServiceConnected(ComponentName name, IBinder service) {
			LocalBinder binder = (LocalBinder) service;
			Log.v(TAG, "service connected");
			mService = binder.getService();
			init();
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub

		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mContentView = inflater.inflate(R.layout.setting_layout, container,
				false);
		Intent serviceIntent = new Intent(getActivity(), LockService.class);
		getActivity().bindService(serviceIntent, mServiceConnection,
				Activity.BIND_AUTO_CREATE);
		mTxvPattern = (TextViewPlus) mContentView
				.findViewById(R.id.txv_pattern);
		mSettingData = SettingData.getInstance(getActivity());
		// mTxvPattern.setOnClickListener(new OnClickListener() {
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// Intent intent = new Intent(
		// LockPatternActivity.ACTION_CREATE_PATTERN, null,
		// SettingFragment.this.getActivity(),
		// LockPatternActivity.class);
		// SettingFragment.this.getActivity().startActivityForResult(
		// intent, REQ_CREATE_PATTERN);
		// }
		// });
		return mContentView;
		// get a list of installed apps.
	}

	private void init() {
		mRlRequirePassword = (RelativeLayout) mContentView
				.findViewById(R.id.rl_require_password);
		mRlSetting = (RelativeLayout) mContentView
				.findViewById(R.id.rl_time_setting);
		mRlSetting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// if (mTbSetting.getVisibility() == View.VISIBLE) {
				// mTbSetting.setVisibility(View.GONE);
				// } else {
				// mTbSetting.setVisibility(View.VISIBLE);
				// }
				// mRbTimeSetting.setChecked(true);
				Toast.makeText(
						getActivity(),
						getResources().getString(R.string.function_not_support),
						Toast.LENGTH_LONG).show();
			}
		});
		mLlExitSetting = (LinearLayout) mContentView
				.findViewById(R.id.ll_exit_setting);
		mLnRateForUS = (LinearLayout) mContentView
				.findViewById(R.id.ll_rate_for_us);
		mTxvHidePhotos=(TextViewPlus)mContentView
				.findViewById(R.id.txv_lock_photos);
		mTxvHidePhotos.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(Intent.ACTION_VIEW, Uri
						.parse("market://details?id=" + "com.vdg.lockphotos")));
			}
		});
		mLnRateForUS.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(Intent.ACTION_VIEW, Uri
						.parse("market://details?id=" + "com.vdg.lockapp")));
			}
		});
		mLlExitSetting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// mRbExitSetting.setChecked(true);
				Toast.makeText(
						getActivity(),
						getResources().getString(R.string.function_not_support),
						Toast.LENGTH_LONG).show();
			}
		});
		mLlHomeSetting = (LinearLayout) mContentView
				.findViewById(R.id.ll_home_setting);
		mLlHomeSetting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mRbHomeSetting.setChecked(true);
			}
		});
		mRlFakeForceClose = (RelativeLayout) mContentView
				.findViewById(R.id.rl_fake_force_close);
		mRlFakeForceClose.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mRbFakeForceClose.setChecked(true);
			}
		});
		mRbFakeForceClose = (RadioButton) mContentView
				.findViewById(R.id.rb_fake_force_close);
		mRbFakeForceClose
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked) {
							mRbLockPatternSetting.setChecked(false);
							mRbPassCodeSetting.setChecked(false);
							mTbPassCode.setVisibility(View.GONE);
							mTbLockPatternSetting.setVisibility(View.GONE);
							mSettingData.setLockType(
									LOCK_TYPE.FAKE_FORCE_CLOSE, getActivity());
						}
					}
				});
		mRbPassCodeSetting = (RadioButton) mContentView
				.findViewById(R.id.rb_passcode);
		mRlPassCodeSetting = (RelativeLayout) mContentView
				.findViewById(R.id.rl_passcode);
		mTbPassCode = (TableLayout) mContentView
				.findViewById(R.id.tl_pass_code_setting);
		mTrChangePassWord = (TableRow) mContentView
				.findViewById(R.id.tr_change_passcode);
		mTrChangePassWord.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						PassCodeActivity.class);
				intent.putExtra(PassCodeActivity.PASS_CODE_ACTION,
						PASS_CODE_ACTION_TYPE.CHANGE_PASSWORD_ACTION.toString());
				startActivity(intent);
			}
		});
		mRlPassCodeSetting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				if (mTbPassCode.getVisibility() == View.VISIBLE) {
					mTbPassCode.setVisibility(View.GONE);
				} else {
					mTbPassCode.setVisibility(View.VISIBLE);
				}
				mRbPassCodeSetting.setChecked(true);
			}
		});
		mRbPassCodeSetting
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked) {
							mRbLockPatternSetting.setChecked(false);
							mRbFakeForceClose.setChecked(false);
							mTbPassCode.setVisibility(View.VISIBLE);
							mSettingData.setLockType(LOCK_TYPE.PASS_CODE,
									getActivity());
						} else {
							mTbPassCode.setVisibility(View.GONE);
						}
					}
				});
		mRlLockPatternSetting = (RelativeLayout) mContentView
				.findViewById(R.id.rl_lock_patten_setting);
		mRbLockPatternSetting = (RadioButton) mContentView
				.findViewById(R.id.rb_lock_pattern);
		mChbRequirePassword = (CheckBox) mContentView
				.findViewById(R.id.chb_require_password);
		mChbRequirePassword
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						mSettingData.setRequirePassword(isChecked,
								getActivity());
					}
				});
		mTbSetting = (TableLayout) mContentView
				.findViewById(R.id.tl_time_setting);
		mRbExitSetting = (RadioButton) mContentView
				.findViewById(R.id.rb_exit_setting);
		mRbHomeSetting = (RadioButton) mContentView
				.findViewById(R.id.rb_home_setting);
		mRbTimeSetting = (RadioButton) mContentView
				.findViewById(R.id.rb_time_setting);
		mTbLockPatternSetting = (TableLayout) mContentView
				.findViewById(R.id.tl_lock_pattern_setting);
		mRlLockPatternSetting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// if (mTbLockPatternSetting.getVisibility() == View.VISIBLE) {
				// mTbLockPatternSetting.setVisibility(View.GONE);
				// } else {
				// mTbLockPatternSetting.setVisibility(View.VISIBLE);
				// }
				// mRbLockPatternSetting.setChecked(true);
				Toast.makeText(
						getActivity(),
						getResources().getString(R.string.function_not_support),
						Toast.LENGTH_LONG).show();
			}
		});
		mRbLockPatternSetting.setEnabled(false);
		mRbLockPatternSetting
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						// if (isChecked) {
						// mRbPassCodeSetting.setChecked(false);
						// mRbFakeForceClose.setChecked(false);
						// mTbLockPatternSetting.setVisibility(View.VISIBLE);
						// mSettingData.setLockType(LOCK_TYPE.LOCK_PATTERM,
						// getActivity());
						// } else {
						// mTbLockPatternSetting.setVisibility(View.GONE);
						// }
						Toast.makeText(
								getActivity(),
								getResources().getString(
										R.string.function_not_support),
								Toast.LENGTH_LONG).show();
					}
				});
		mRbExitSetting.setEnabled(false);
		mRbExitSetting
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						// if (isChecked == true) {
						// mSettingData.setLockSetting(
						// LOCK_SETTING_TYPE.CLOSE_APP, getActivity());
						// mRbHomeSetting.setChecked(false);
						// mRbTimeSetting.setChecked(false);
						// }
						Toast.makeText(
								getActivity(),
								getResources().getString(
										R.string.function_not_support),
								Toast.LENGTH_LONG).show();
					}
				});
		mRbTimeSetting.setEnabled(false);
		mRbTimeSetting
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						// if (isChecked == true) {
						// mSettingData.setLockSetting(LOCK_SETTING_TYPE.TIME,
						// getActivity());
						// mRbHomeSetting.setChecked(false);
						// mRbExitSetting.setChecked(false);
						// mTbSetting.setVisibility(View.VISIBLE);
						// } else {
						// mTbSetting.setVisibility(View.GONE);
						// }
						Toast.makeText(
								getActivity(),
								getResources().getString(
										R.string.function_not_support),
								Toast.LENGTH_LONG).show();
					}
				});
		mRbHomeSetting
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked == true) {
							mSettingData.setLockSetting(LOCK_SETTING_TYPE.HOME,
									getActivity());
							mRbTimeSetting.setChecked(false);
							mRbExitSetting.setChecked(false);
						}
					}
				});
		mRlRequirePassword.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mChbRequirePassword.setChecked(!mChbRequirePassword.isChecked());
			}
		});
		loadSetting();
	}

	public void loadSetting() {
		if (mSettingData.getLockType() == null) {
			mSettingData.setLockType(LOCK_TYPE.PASS_CODE, getActivity());
		}
		if (mSettingData.getLockType() == LOCK_TYPE.PASS_CODE) {
			mRbPassCodeSetting.setChecked(true);
		} else {
			if (mSettingData.getLockType() == LOCK_TYPE.LOCK_PATTERM) {
				mRbLockPatternSetting.setChecked(true);
			} else {
				mRbFakeForceClose.setChecked(true);
			}
		}
		if (mSettingData.getLockSetting() == LOCK_SETTING_TYPE.TIME) {
			mRbTimeSetting.setChecked(true);
		} else {
			if (mSettingData.getLockSetting() == LOCK_SETTING_TYPE.HOME) {
				mRbHomeSetting.setChecked(true);
			} else {
				mRbExitSetting.setChecked(true);
			}
		}
		if (mSettingData.getRequirePassword()) {
			mChbRequirePassword.setChecked(true);
		} else {
			mChbRequirePassword.setChecked(false);
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		Log.v(TAG, "onactivity result");
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		getActivity().unbindService(mServiceConnection);
		super.onDestroy();

	}
}
